package cn.ecsd.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.ecsd.entity.BizLeave;
import cn.ecsd.entity.SysEmployee;
import cn.ecsd.service.EmployeeService;
import cn.ecsd.service.LeaveService;
import cn.ecsd.util.PageSupport;

/**
 * 请假的action类
 * 
 * @author 寇超强
 *
 */
public class LeaveAction extends ActionSupport {
	private LeaveService leaveService;
	private EmployeeService empService;
	private Integer pageNo;
	private Integer pageSize;
	private PageSupport page = new PageSupport();
	private BizLeave leave;
	private SysEmployee manager;

	public SysEmployee getManager() {
		return manager;
	}

	public void setManager(SysEmployee manager) {
		this.manager = manager;
	}

	public EmployeeService getEmpService() {
		return empService;
	}

	public void setEmpService(EmployeeService empService) {
		this.empService = empService;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public LeaveService getLeaveService() {
		return leaveService;
	}

	public void setLeaveService(LeaveService leaveService) {
		this.leaveService = leaveService;
	}

	public BizLeave getLeave() {
		return leave;
	}

	public void setLeave(BizLeave leave) {
		this.leave = leave;
	}

	public PageSupport getPage() {
		return page;
	}

	public void setPage(PageSupport page) {
		this.page = page;
	}

	Map<String, Object> session = ActionContext.getContext().getSession();
	
	/**
	 * 查看请假列表
	 * 
	 * @return
	 */
	public String searchLeave() {
		if (pageNo != null) {
			page.setCurrentPageNo(pageNo);
		}
		page = leaveService.getLeavePage(page, leave);
		session.put("pageSupport", page);
		return "ShowLeavelist";
	}

	/**
	 * 查看请假详情
	 * 
	 * @return
	 */
	public String getLeaveById() {
		leave = leaveService.getLeaveById(leave.getId());
		return "view";
	}

	/**
	 * 查看 请假审批
	 */
	public String toCheck() {
		leave = leaveService.getLeaveById(leave.getId());
		return "tocheck";
	}

	/**
	 * 请假审批
	 * 
	 * @return
	 */
	public String checkLeave() {

		BizLeave leave2 = leaveService.getLeaveById(leave.getId());
		leave2.setStatus(leave.getStatus());
		leave2.setModifytime(new Timestamp(new Date().getTime()));
		leave2.setApproveOpinion(leave.getApproveOpinion());
		
		leave.setModifytime(new Timestamp(new Date().getTime()));
		leaveService.updatestatus(leave2);
		return "checkLeave";
	}

	/**
	 * 显示申请页面
	 * 
	 * @return
	 */
	public String toEdit() {
		manager = empService.getManagerByDeparId((SysEmployee) session.get("employee"));
		session.put("manager", manager);
		return "toEdit";
	}

	/**
	 * 保存新增请假信息
	 */
	public String saveLeave() {
		leave.setStatus("待审批");
		leave.setCreatetime(new Timestamp(new Date().getTime()));
		leave.setSysEmployeeByEmployeeSn((SysEmployee) session.get("employee"));
		leave.setSysEmployeeByNextDealSn((SysEmployee) session.get("manager"));
		leaveService.addLeave(leave);
		return "saveLeave";
	}
}
